package com.example.blogdesign.Service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blogdesign.Entity.UserInfo;
import com.example.blogdesign.Mapper.UserInfoMapper;
import com.example.blogdesign.Service.IUserInfoService;
import com.example.blogdesign.dto.Result;
import org.springframework.stereotype.Service;


@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    @Override
    public Result<?> editUserInfo(UserInfo userInfo) {
        boolean isSuccess = updateById(userInfo);
        if (isSuccess){
            return Result.ok("修改成功");
        }
        return Result.fail("修改失败");
    }

    @Override
    public Result<UserInfo> queryById(Long userId) {
        // 查询详情
        UserInfo userInfo = getById(userId);
        if (userInfo == null) {
            // 没有详情，应该是第一次查看详情
            return Result.ok();
        }
        // 返回
        return Result.ok(userInfo);
    }

    //修改粉丝数量
    @Override
    public void modifyFansNumber(Long followUserId,Boolean isFollow){
        if (isFollow){
            //关注，让被关注人的粉丝+1
            update().setSql("fans=fans+1").eq("user_id",followUserId).update();
        }else {
            //取关，粉丝-1
            update().setSql("fans=fans-1").eq("user_id",followUserId).update();
        }
    }


    //修改用户关注人数
    @Override
    public void modifyFollowNumber(Long currentUserId ,Boolean isFollow){
        if (isFollow){
            //关注，当前用户关注人数+1
            update().setSql("followee=followee+1").eq("user_id",currentUserId).update();
        }else {
            //取关，关注人数-1
            update().setSql("follow=follow-1").eq("user_id",currentUserId).update();
        }
    }

}
